﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.Common;
using System.IO;
using BarCode.Module;
using BarCode.AccessDao;
using System.Runtime.InteropServices;
using BarCode.Bean;

namespace BarCode.Forms
{
    public partial class TMZBarCode : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Base_BarCode));


        private string jsq_barcodeID;
        private string mp_barcodeID;

        [DllImport("user32.dll")]
        public static extern bool MessageBeep(uint uType);

        private int workAmount;

        public TMZBarCode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_Load(object sender, EventArgs e)
        {
            lblPreUser.Text = "";
            lblPreUser.ForeColor = Color.Blue;
            lblUser.Text = GlobalVal.CurrentUser.User;
            txtBarCode.Focus();

            // 获取当前用户当天的工作量
            DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            workAmount = db01.getTMZWorkAmount(GlobalVal.CurrentUser.User);
            lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();

            txtBarCode.Text = "";
            txtBarCode.Focus();
            lblResult.Text = "";
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
        }


        /// <summary>
        /// 二维码输入后回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)13)
                {
                    lblStatus.Text = "";
                    lblPreUser.ForeColor = Color.Blue;
                    lblPreUser.Text = "";
                    string inputBarCode = txtBarCode.Text;
                    lblResult.Text = "";
                    if (inputBarCode.Length != 12)
                    {
                        MessageUtil.Warn("条码长度不正确!");
                        txtBarCode.Text = "";
                        ComUtil.sendFail(workAmount);
                        return;
                    }

                    DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                    if (inputBarCode.StartsWith("7"))
                    {
                        // 保存计数器码
                        jsq_barcodeID = inputBarCode;
                        lblStatus.Text = "请扫描铭牌码";

                        txtBarCode.Text = "";
                        txtBarCode.Focus();

                        lblAmount.ForeColor = Color.Red;
                        ComUtil.sendInputing(workAmount);
                        return;
                    }

                    // 通过表号查询前道工序
                    GasCodeInfo gasInfo = db01.getGasInfo(inputBarCode);
                    if (gasInfo == null)
                    {
                        // 表号不存在
                        lblResult.Text = "表号不存在";
                        txtBarCode.Text = "";
                        txtBarCode.Focus();
                        jsq_barcodeID = "";

                        ComUtil.sendFail(workAmount);
                        return;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(jsq_barcodeID))
                        {
                            lblStatus.Text = "请先扫描计数器码！";
                            lblResult.Text = "先扫计数器";
                            txtBarCode.Text = "";
                            txtBarCode.Focus();
                            ComUtil.sendFail(workAmount);
                            return;
                        }

                        // 计数器码不一致
                        if (!jsq_barcodeID.Equals(gasInfo.body_no))
                        {
                            lblResult.Text = "表号错误";
                            txtBarCode.Text = "";
                            txtBarCode.Focus();
                            jsq_barcodeID = "";

                            ComUtil.sendFail(workAmount);
                            return;
                        }

                        lblPreUser.Text = gasInfo.operator_name;

                        // 保存
                        // 记录
                        db01.saveTMZBarCode(gasInfo);

                        // 获取当前用户当天的工作量
                        workAmount = db01.getTMZWorkAmount(GlobalVal.CurrentUser.User);
                        lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();
                        lblAmount.ForeColor = Color.Black;

                        lblStatus.Text = "请扫描计数器码";
                        ComUtil.sendSuccess(workAmount);

                    }

                    // 清空输入内容
                    txtBarCode.Text = "";
                    txtBarCode.Focus();
                    jsq_barcodeID = "";
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
            }
        }

        /// <summary>
        /// 输入有误时弹框显示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowInputWarn(string msg, uint beep)
        {
            txtBarCode.Text = "";
            txtBarCode.Focus();
           
            if (beep > 0)
            {
                MessageBeep(beep);
            }
            MessageUtil.Warn(msg);
        }
    }
}
